package com.sw.chinesewriteboard.operation;

import android.content.Context;

import com.sw.chinesewriteboard.config.EnglishDataConfig;

import java.util.List;

public class EnglishDataOperate {

    ArrangeType arrangeType;

    public enum ArrangeType {
        CHARACTER,
        WORD,
        PRACTISE
    }


    private Context mContext;
    private static int rowEmpty = 0;

    private static float divison = 1; // 全描，半描，不描

    public EnglishDataOperate(Context context) {
        mContext = context;
        rowEmpty = 0;
        divison = 1;
    }

    public void setArrangeType(ArrangeType arrangeType) {
        this.arrangeType = arrangeType;
    }

    public String listToString(List<String> list) {

        final int COLUMN_LENGTH = EnglishDataConfig.TOTAL_LENGTH_COLUMN;
        final int ROW_LENGTH = EnglishDataConfig.TOTAL_LENGTH_ROW;

        StringBuilder temp = new StringBuilder();
        if (list.isEmpty()) {
            int surplus = EnglishDataConfig.TOTAL_LENGTH - temp.length();
            addSpace(surplus, temp);
            return temp.toString();
        }
        switch (arrangeType) {
            case CHARACTER:
                for (String words : list) {
                    for (int i = 0; i < words.length(); i++) {
                        temp.append(words.charAt(i));
                        for (int j = 0; j < COLUMN_LENGTH - 1; j++) {
                            temp.append(' ');
                        }
                        for (int k = 0; k < rowEmpty; k++) {
                            for (int j = 0; j < COLUMN_LENGTH; j++) {
                                temp.append(' ');
                            }
                        }
                    }
                }
                break;

            case WORD:
                for (String word : list) {
                    temp.append(word);
                    for (int j = 0; j < COLUMN_LENGTH - word.length(); j++) {
                        temp.append(' ');
                    }
                    for (int k = 0; k < rowEmpty; k++) {
                        for (int j = 0; j < COLUMN_LENGTH; j++) {
                            temp.append(' ');
                        }
                    }
                }
                break;
            case PRACTISE:
                for (String words : list) {
                    for (int i = 0; i < words.length(); i++) {
                        temp.append(words.charAt(i));
                        int miao = (int) ((COLUMN_LENGTH - 1) * divison);
                        for (int j = 0; j < miao; j++) {
                            temp.append(words.charAt(i));
                        }
                        for (int l = 0; l < COLUMN_LENGTH - 1 - miao; l++) {
                            temp.append(" ");
                        }
                        for (int k = 0; k < rowEmpty; k++) {
                            for (int j = 0; j < COLUMN_LENGTH; j++) {
                                temp.append(' ');
                            }
                        }
                    }
                }
                break;
        }

        // 字帖页面上的剩余空格补足
        int surplus = EnglishDataConfig.TOTAL_LENGTH - temp.length();
        addSpace(surplus, temp);
        return temp.toString();
    }

    /*
    往后增加空格
     */
    private StringBuilder addSpace(int surplus, StringBuilder temp) {
        while (surplus > 0) {
            temp.append(' ');
            surplus--;
        }
        return temp;
    }


    public int getRowEmpty() {
        return rowEmpty;
    }

    public static void setRowEmpty(int rowEmpty) {
        EnglishDataOperate.rowEmpty = rowEmpty;
    }

    public static float getDivison() {
        return divison;
    }

    public static void setDivison(float divison) {
        EnglishDataOperate.divison = divison;
    }

    /*
            检查是否有标点符号
             */
    public boolean checkHasPunctuation(String s) {
        boolean b = false;

        String tmp = s;
        tmp = tmp.replaceAll("\\p{P}", "");
        if (s.length() != tmp.length()) {
            b = true;
        }

        return b;
    }
}
